//
//  oldChooseCollectionViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/11/5.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldChooseCollectionViewController.h"
#import "oldChooseCollectionViewCell.h"
#import "oldNetWork.h"
#import "oldHub.h"
#import "oldSubServicesViewController.h"

@interface oldChooseCollectionViewController ()
@property (nonatomic, copy)NSArray *dataSource;
@end

@implementation oldChooseCollectionViewController

static NSString * const reuseIdentifier = @"oldChooseCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"选择服务类型";
    UIBarButtonItem *backIteme = [[UIBarButtonItem alloc]init];
    backIteme.title = @" ";
    self.navigationItem.backBarButtonItem = backIteme;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"oldChooseCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initData
- (void)initData
{
    WeakSelf
    
    [oldHub waiting:@"载入中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_GetServeCategory params:@{@"pid" : @"0"} success:^(NSDictionary *dic) {
        [oldHub hideHUD];
        [weakSelf initDataSource:dic];
        [weakSelf.collectionView reloadData];
    } error:^(NSString *error) {
        [oldHub hideHUD];
    }];
}

- (void)initDataSource:(id)data
{
    _dataSource = data;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return ([_dataSource count] + 1)/2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (([_dataSource count] > 2 * section) && ([_dataSource count] < 2 * (section + 1))) {
        return 1;
    }

    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    oldChooseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSUInteger index = indexPath.section * 2 + indexPath.row;
    
    NSString *title = _dataSource[index][@"cname"];
    
    cell.label_title.text = title;
    [cell.img showImage:_dataSource[index][@"pic"]];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (SCREEN_WIDTH - 26*2 - 22)/2;
    
    return CGSizeMake(width, width);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 22;
}

//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 26, 10, 26);
}


//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 24;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger index = indexPath.section * 2 + indexPath.row;
    
    NSString *cid = _dataSource[index][@"cid"];
    NSString *cname = _dataSource[index][@"cname"];
    
    oldSubServicesViewController *vc = [oldSubServicesViewController new];
    vc.pid = cid;
    vc.cname = cname;
    [self.navigationController pushViewController:vc animated:YES];
}

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
